<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    //
    private $request;
    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->infoLog($this->request);
    }


    /**
     * 返回json数据
     *
     * @param $data 返回数据
     * @param int $status_code 状态码
     * @return \Illuminate\Http\JsonResponse
     */
    public function respond($status_code = 200, $data = [])
    {
        return response()->json(
            [
                'status' => $status_code,
                'msg'    => ($status_code == 200) ? '' : config('lang_'. $this->request->lang.'.'.$status_code),
                'data'   => $data
            ]
        );
    }


    /**
     * 记录请求的日志
     *
     * @param $request
     */
    public function infoLog($request)
    {
        $uri = parse_url($request->server('REQUEST_URI'));
        $ip  = $request->ip();
        //不是本地环境就开启日志
        if(env('APP_ENV') != 'local') Log::info('Log ' . $ip, ['url' =>  $uri['path'], 'params'  =>  $request->all()]);
    }
}
